
// Copyright (C) Recursion Company. All rights reserved.

#ifndef __VOL_WTL_UI_IE_BROWSER_H__
#define __VOL_WTL_UI_IE_BROWSER_H__

class IIEBrowserEventListener : public IBaseEventListener
{
public:
    virtual void OnProgressChange (INT nProgress) = 0;
    virtual void OnCommandStateChange (INT nCommand, BOOL blEnabled) = 0;
    virtual void OnDownloadBegin () = 0;
    virtual void OnDocumentComplete (CVolString& strURL) = 0;
    virtual void OnDownloadComplete () = 0;
    virtual void OnTitleChange (CVolString& strTitle) = 0;
    virtual BOOL OnNewWindow (CVolString& strURL) = 0;
    virtual BOOL OnBeforeNavigate (CVolString& strURL) = 0;
    virtual void OnStatusTextChange (CVolString& strStatusText) = 0;
    virtual void OnNavigateComplete (CVolString& strURL) = 0;
};

#define _VOL_OLE_TRUE  (-1)
#define _VOL_OLE_FALSE  0

#define _VOL_IE_BROWSER_CONTROL_ID  0xE900

class CVWnd_IEBrowser :
        public CWindowImpl<CVWnd_IEBrowser, CAxWindow2>,
        public IDispEventImpl<_VOL_IE_BROWSER_CONTROL_ID, CVWnd_IEBrowser>
{
    BEGIN_DECLARE_VOL_NATIVE_WND_CLASS (CVWnd_IEBrowser, IIEBrowserEventListener, FALSE)
        MSG_WM_DESTROY (OnDestroy)
        MSG_WM_SIZE (OnSize)
        MSG_WM_SETFOCUS (OnSetFocus)
    END_DECLARE_VOL_NATIVE_WND_CLASS_NOT_CONTAINER ()
    DECLARE_WND_SUPERCLASS (NULL, CAxWindow2T::GetWndClassName ())

    BEGIN_SINK_MAP (CVWnd_IEBrowser)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 102, OnStatusTextChange, &CVWnd_IEBrowser::s_infOnStatusTextChange)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 108, OnProgressChange, &CVWnd_IEBrowser::s_infOnProgressChange)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 105, OnCommandStateChange, &CVWnd_IEBrowser::s_infOnCommandStateChange)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 106, OnDownloadBegin, &CVWnd_IEBrowser::s_infOnDownloadBegin)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 104, OnDownloadComplete, &CVWnd_IEBrowser::s_infOnDownloadComplete)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 113, OnTitleChange, &CVWnd_IEBrowser::s_infOnTitleChange)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 252, OnNavigateComplete2, &CVWnd_IEBrowser::s_infOnNavigateComplete2)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 250, OnBeforeNavigate2, &CVWnd_IEBrowser::s_infOnBeforeNavigate2)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 251, OnNewWindow2, &CVWnd_IEBrowser::s_infOnNewWindow2)
        SINK_ENTRY_INFO (_VOL_IE_BROWSER_CONTROL_ID, IID_NULL, 259, OnDocumentComplete, &CVWnd_IEBrowser::s_infOnDocumentComplete)
    END_SINK_MAP ()

public:
    CVWnd_IEBrowser ();
    ~CVWnd_IEBrowser ();

public:
    inline_ IWebBrowser2* GetBrowser() { return m_pBrowserApp; }
    CVolComObject& GetBrowserIDispathObject (CVolComObject& objCom);
    CVolComObject& GetDocumentIDispathObject (CVolComObject& objCom);
                
    CVolString GetType() const;
    BOOL GetBusy() const;
    READYSTATE GetReadyState() const;
    CVolString GetLocationName() const;
    long GetLeft() const;
    long GetTop() const;
    long GetHeight() const;
    BOOL GetVisible() const;
    BOOL GetOffline() const;
    BOOL GetSilent() const;
    CVolString GetLocationURL() const;

    inline_ void SetLeft(long nNewValue) { if (m_pBrowserApp != NULL) m_pBrowserApp->put_Left(nNewValue); }
    inline_ void SetTop(long nNewValue) { if (m_pBrowserApp != NULL) m_pBrowserApp->put_Top(nNewValue); }
    inline_ void SetHeight(long nNewValue) { if (m_pBrowserApp != NULL) m_pBrowserApp->put_Height(nNewValue); }
    inline_ void SetVisible(BOOL fNewValue) { if (m_pBrowserApp != NULL) m_pBrowserApp->put_Visible((short) (fNewValue ? _VOL_OLE_TRUE : _VOL_OLE_FALSE)); }
    inline_ void SetOffline(BOOL fNewValue) { if (m_pBrowserApp != NULL) m_pBrowserApp->put_Offline((short) (fNewValue ? _VOL_OLE_TRUE : _VOL_OLE_FALSE)); }
    inline_ void SetSilent(BOOL fNewValue) { if (m_pBrowserApp != NULL) m_pBrowserApp->put_Silent((short) (fNewValue ? _VOL_OLE_TRUE : _VOL_OLE_FALSE)); }

    inline_ void GoBack() { if (m_pBrowserApp != NULL) m_pBrowserApp->GoBack(); }
    inline_ void GoForward() { if (m_pBrowserApp != NULL) m_pBrowserApp->GoForward(); }
    inline_ void GoHome() { if (m_pBrowserApp != NULL) m_pBrowserApp->GoHome(); }
    inline_ void GoSearch() { if (m_pBrowserApp != NULL) m_pBrowserApp->GoSearch(); }
    inline_ void Refresh() { if (m_pBrowserApp != NULL) m_pBrowserApp->Refresh(); }
    inline_ void Stop() { if (m_pBrowserApp != NULL) m_pBrowserApp->Stop(); }

    void RunCmd (OLECMDID cmdid);
    void ExecWB(OLECMDID cmdID, OLECMDEXECOPT cmdexecopt, VARIANT* pvaIn, VARIANT* pvaOut);
    HRESULT Navigate (LPCTSTR URL, DWORD dwFlags = 0, LPCTSTR lpszTargetFrameName = NULL,
            LPCTSTR lpszHeaders = NULL, LPVOID lpvPostData = NULL, DWORD dwPostDataLen = 0);
    HRESULT NavigateChm(LPCTSTR szChmFileName, LPCTSTR URL, LPCTSTR lpszTargetFrameName = NULL);

    void PropSetFontSize (INT nFontSize);
    void PropSetOffline (BOOL blOffline);
    void PropSetSilent (BOOL blSilent);
    void PropSetURL (const TCHAR* szURL);

    inline_ INT PropGetFontSuze () const  { return m_nFontSize; } 
    BOOL PropGetOffline () const;
    BOOL PropGetSilent () const;
    CVolString PropGetURL () const;

    // Events
    void __stdcall OnStatusTextChange (LPCTSTR lpszText);
    void __stdcall OnProgressChange (long nProgress, long nProgressMax);
    void __stdcall OnCommandStateChange (long nCommand, BOOL fEnable);
    void __stdcall OnDownloadBegin ();
    void __stdcall OnDownloadComplete ();
    void __stdcall OnTitleChange (LPCTSTR lpszText);
    void __stdcall OnNavigateComplete2 (LPDISPATCH pDisp, VARIANT* URL);
	void __stdcall OnBeforeNavigate2 (LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, VARIANT_BOOL* Cancel);
    void __stdcall OnNewWindow2 (LPDISPATCH* ppDisp, BOOL* Cancel);
    void __stdcall OnDocumentComplete (LPDISPATCH pDisp, VARIANT* URL);

protected:
    void OnSetFocus (CWindow wndOld);
    void OnSize (UINT nType, CSize size);
    void OnDestroy ();

protected:
	CAxWindow m_wndBrowser;
    IWebBrowser2* m_pBrowserApp;

	INT m_nFontSize;
    BOOL m_blOffline;
    BOOL m_blSilent;
	CVolString m_strURL;

    static _ATL_FUNC_INFO s_infOnStatusTextChange, s_infOnProgressChange, s_infOnCommandStateChange, s_infOnDownloadBegin, s_infOnDownloadComplete,
            s_infOnTitleChange, s_infOnNavigateComplete2, s_infOnBeforeNavigate2, s_infOnNewWindow2, s_infOnDocumentComplete;
};

#endif
